﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Project.Model;
using Project.Controller;
using Project.View;
using System.IO;
using System.Xml;

namespace Project.UserControls
{
    public partial class Spel_UserControl : UserControl, SpelUserControlObserver
    {
        private Spel spel;
        private Log log;
        private Form form;

        public Form Form
        {
            get { return form; }
            set { form = value; }
        }

        public Spel_UserControl(Speler[] spelers, Form form)
        {
            InitializeComponent();
            this.Location = new Point(Location.X, Location.Y + SystemInformation.MenuHeight);
            this.spel = new Spel(spelers);
            this.spel.RegisterObserver(this);
            bool logOnly;
            if (spelers.Count() == 4)
            {
                logOnly = false;
            }
            else
            {
                logOnly = true;
            }
            this.log = new Log(this.spel, form, logOnly);
            this.spel.Log = this.log;
            if (!logOnly)
            {
                CreateBord();
                UpdateBord();
                this.gooi_button.Text = "Start spel";
                this.gooi_button.Click -= this.gooi_button_Click;
                this.gooi_button.Click += new System.EventHandler(this.startSpel);
            }
            this.form = form;
        }

        private void CreateBord()
        {
            Vak v = this.spel.Bord.First;
            int count = 0;
            int count2 = 0;
            bool run = false;
            bool bln = true;
            int x = 360;
            int y = 650;
            while (bln)
            {
                Color panelColor = System.Drawing.Color.Black;
                if (v.VakType == VakType.Vak_A)
                {
                    panelColor = this.spel.Spelers[count2].Color;
                }
                VakPanel vakPanel = new VakPanel(panelColor);
                v.VakPanel = vakPanel;

                if (count == 2 || count == 10)
                {
                    run = true;
                }
                if (count == 6)
                {
                    run = false;
                }

                for (int i = 0; i < 4; i++)
                {
                    switch (i)
                    {
                        case 0:
                            Label label1 = new Label();
                            label1.BackColor = System.Drawing.Color.Transparent;
                            label1.Location = new Point(20, 560);
                            label1.Text = this.spel.Spelers[i].Naam;
                            label1.Font = new Font(this.Font.Name, 12, FontStyle.Bold);
                            this.Controls.Add(label1);
                            break;
                        case 1:
                            Label label2 = new Label();
                            label2.BackColor = System.Drawing.Color.Transparent;
                            label2.Location = new Point(20, 20);
                            label2.Text = this.spel.Spelers[i].Naam;
                            label2.Font = new Font(this.Font.Name, 12, FontStyle.Bold);
                            this.Controls.Add(label2);
                            break;
                        case 2:
                            Label label4 = new Label();
                            label4.BackColor = System.Drawing.Color.Transparent;
                            label4.Location = new Point(560, 20);
                            label4.Text = this.spel.Spelers[i].Naam;
                            label4.Font = new Font(this.Font.Name, 12, FontStyle.Bold);
                            this.Controls.Add(label4);
                            break;
                        case 3:
                            Label label3 = new Label();
                            label3.BackColor = System.Drawing.Color.Transparent;
                            label3.Location = new Point(560, 560);
                            label3.Text = this.spel.Spelers[i].Naam;
                            label3.Font = new Font(this.Font.Name, 12, FontStyle.Bold);
                            this.Controls.Add(label3);
                            break;
                    }
                }

                if(v.NextEind != null) {
                    for (var e = 1; e <= 4; e++)
                    {
                        VakPanel bpanel = new VakPanel(this.spel.Spelers[count2].Color);
                        switch (count2)
                        {
                            case 0:
                                bpanel.Location = new Point(x, y - (60 * e));
                                break;
                            case 1:
                                bpanel.Location = new Point(x + (60 * e), y);
                                break;
                            case 2:
                                bpanel.Location = new Point(x, y + (60 * e));
                                break;
                            case 3:
                                bpanel.Location = new Point(x - (60 * e), y);
                                break;
                        }
                        this.spel.Spelers[count2].Vak_eind[e - 1].VakPanel = bpanel;
                        this.Controls.Add(bpanel);
                    }
                }

                vakPanel.Location = new Point(x, y);
                this.Controls.Add(vakPanel);

                switch (count2)
                {
                    case 0:
                        if (run)
                        {
                            y -= 60;
                        }
                        else
                        {
                            x -= 60;
                        }
                        break;
                    case 1:
                        if (run)
                        {
                            x += 60;
                        }
                        else
                        {
                            y -= 60;
                        }
                        break;
                    case 2:
                        if (run)
                        {
                            y += 60;
                        }
                        else
                        {
                            x += 60;
                        }
                        break;
                    case 3:
                        if (run)
                        {
                            x -= 60;
                        }
                        else
                        {
                            y += 60;
                        }
                        break;
                }  
                v = v.Next;
                count++;
                if (count == 10)
                {
                    for (var a = 0; a < 2; a++)
                    {
                        for (var b = 0; b < 2; b++)
                        {
                            VakPanel spanel = new VakPanel(this.spel.Spelers[count2].Color);
                            switch (count2)
                            {
                                case 0:
                                    spanel.Location = new Point(x + (b * 60), y + (60 * (3 + a)));
                                    break;
                                case 1:
                                    spanel.Location = new Point(x - (60 * (3 + a)), y + (b * 60));
                                    break;
                                case 2:
                                    spanel.Location = new Point(x - (b * 60), y - (60 * (3 + a)));
                                    break;
                                case 3:
                                    spanel.Location = new Point(x + (60 * (3 + a)), y - (b * 60));
                                    break;
                            }
                            this.spel.Spelers[count2].Vak_b[b + (a * 2)].VakPanel = spanel;
                            this.Controls.Add(spanel);
                        }
                    }
                    count2++;
                    count = 0;
                }
                if (v == this.spel.Bord.First)
                {
                    bln = false;
                }
            }
        }

        public void UpdateBord()
        {
            updateBordVeld();
            updateBordBeginvakken();
            updateBordEindvakken();
            if (this.spel.getHuidigeSpeler() is Bot)
            {
                this.huidigeSpeler_naam.Text = this.spel.getHuidigeSpeler().Naam + " (" + ((Bot)this.spel.getHuidigeSpeler()).getMoeilijkheid() + ")";
            }
            else
            {
                this.huidigeSpeler_naam.Text = this.spel.getHuidigeSpeler().Naam;
            }
            this.huidigeSpeler_naam.BackColor = this.spel.getHuidigeSpeler().Color;
            this.worp_label.Text = this.spel.Status;
        }

        private void updateBordVeld()
        {
            Vak vak = this.spel.Bord.First;
            bool bln = true;
            while (bln)
            {
                vak.VakPanel.Controls.Clear();
                if (vak.heeftPion())
                {
                    vak.VakPanel.Controls.Add(vak.Pion.PionPanel);
                }
                vak = vak.Next;
                if (vak == this.spel.Bord.First)
                {
                    bln = false;
                }
            }
        }

        private void updateBordEindvakken()
        {
            for (int i = 0; i < this.spel.Spelers.Count(); i++)
            {
                Speler speler = this.spel.Spelers[i];
                Vak[] vakken = speler.Vak_eind;
                for (int j = 0; j < 4; j++)
                {
                    Vak vak = vakken[j];
                    vak.VakPanel.Controls.Clear();
                    if (vak.heeftPion())
                    {
                        vak.VakPanel.Controls.Add(vak.Pion.PionPanel);
                    }
                }
            }
        }

        private void updateBordBeginvakken()
        {
            for (int i = 0; i < this.spel.Spelers.Count(); i++)
            {
                Speler speler = this.spel.Spelers[i];
                Vak[] vakken = speler.Vak_b;
                for (int j = 0; j < 4; j++)
                {
                    Vak vak = vakken[j];
                    vak.VakPanel.Controls.Clear();
                    if (vak.heeftPion())
                    {
                        vak.VakPanel.Controls.Add(vak.Pion.PionPanel);
                    }
                }
            }
        }

        private void gooi_button_Click(object sender, EventArgs e)
        {
            this.spel.gooi();
            UpdateDiceImage();
        }

        private void startSpel(object sender, EventArgs e)
        {
            this.spel.start();
            setGooiButton();
        }

        public void setGooiButton()
        {
            this.gooi_button.Text = "Gooi";
            this.gooi_button.Click -= this.startSpel;
            this.gooi_button.Click += new System.EventHandler(this.gooi_button_Click);
        }

        public void UpdateDiceImage()
        {
            switch (this.spel.Worp)
            {
                case 1:
                    this.dice_pictureBox.Image = global::Project.Properties.Resources._1;
                    break;
                case 2:
                    this.dice_pictureBox.Image = global::Project.Properties.Resources._2;
                    break;
                case 3:
                    this.dice_pictureBox.Image = global::Project.Properties.Resources._3;
                    break;
                case 4:
                    this.dice_pictureBox.Image = global::Project.Properties.Resources._4;
                    break;
                case 5:
                    this.dice_pictureBox.Image = global::Project.Properties.Resources._5;
                    break;
                case 6:
                    this.dice_pictureBox.Image = global::Project.Properties.Resources._6;
                    break;
            }
        }

        public void UpdateGooiButton(bool enable)
        {
            this.gooi_button.Enabled = enable;
        }

        public Form getForm()
        {
            return form;
        }

        public Spel Spel
        {
            get { return spel; }
            set { spel = value; }
        }
    }
}
